package com.tangji.signaltower.container;

import com.alibaba.fastjson.JSON;
import com.fqgj.base.services.redis.RedisClient;
import com.jsoniter.JsonIterator;
import com.jsoniter.output.JsonStream;
import com.tangji.signaltower.bean.MarketDataEntity;
import com.tangji.signaltower.config.FileConfigUtil;
import com.tangji.signaltower.constant.RedisKey;
import com.tangji.signaltower.enums.ProductEnum;
import com.tangji.signaltower.task.PushDataContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

import static com.tangji.antenna.bean.EventFactory.REFERENCE_VALUE;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2020-07-23
 * Time: 11:28
 */
@Service
public class ProductDataContainer {

    private static Map<ProductEnum, DataContainer> productEnumDataContainerMap = new HashMap<>();
    public static Map<ProductEnum, PushDataContainer> pushDataContainerHashMap = new HashMap<>();

    private static final Logger logger = LoggerFactory.getLogger(ProductDataContainer.class);

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private FileConfigUtil fileConfigUtil;

    @PostConstruct
    public void init() {
        for (ProductEnum productEnum : ProductEnum.values()) {
            String latestValue = redisClient.get(RedisKey.ZSET_BINARY_OPTION_PRODUCT_LAST_KEY, productEnum.getProductCode());
            MarketDataEntity lastData = null;
            if (latestValue != null) {
                logger.info("取最新值,type:{},latestValue:{}", productEnum.getProductCode(), latestValue);
                lastData = JsonIterator.deserialize(latestValue, MarketDataEntity.class);
            }
            if (fileConfigUtil.isServerTest() && lastData == null) {
                long time = System.currentTimeMillis();
                MarketDataEntity mock = new MarketDataEntity(productEnum.getProductCode(), "1.0", time, (time - REFERENCE_VALUE) / 1000, 2);
                lastData = mock;
            }

            DataContainer container = new DataContainer(productEnum.getProductCode(), lastData);
            productEnumDataContainerMap.put(productEnum, container);
            pushDataContainerHashMap.put(productEnum, new PushDataContainer(productEnum.getProductCode(), lastData));
            logger.info("初始化DataContainer完成,{},lastData:{}", productEnum.getProductCode(), JSON.toJSONString(lastData));
        }
    }

    public static DataContainer getDataContainer(ProductEnum productEnum) {
        if (!productEnumDataContainerMap.containsKey(productEnum)) {
            return null;
        }

        return productEnumDataContainerMap.get(productEnum);
    }


    public static void main(String[] args) {
        String latestValue = JsonStream.serialize(new MarketDataEntity());
        MarketDataEntity deserialize = JsonIterator.deserialize(latestValue, MarketDataEntity.class);


        System.err.println(111);

    }

}
